import Vue from 'vue'
import App from './App.vue'

Vue.config.productionTip = false
// 引用vuex步骤
// 1、安装
// 2、 引入
import Vuex from 'vuex'
// 3、注册-应用
Vue.use(Vuex)
// 4、实例化
const store = new Vuex.Store({
  // 这里放配置
  state: {
    name: '学习',
    count: 18,
    list: [1, 2, 3, 4, 5, 6, 7, 8, 9],
    user: {
      userinfo: {
        school: {
          name: '黑马'
        }
      }
    }
  },
  mutations: {
    // 用来修改state的同步函数
    addOne(state, data) {
      state.count += data
    }
  },
  actions: {
    asyncAddOne(store, data) {
      setTimeout(() => {
        store.commit('addOne', data)
      }, 1000)
    }
  },
  getters: {
    filterList: state => state.list.filter(item => item > 5),
    userInfo: state => state.user.userinfo.school.name,

    token: state => state.users.token,
    name: state => state.setting.name
  },
  modules: {
    users: {
      // 开启命名空间限制
      namespaced: true,
      state: {
        token: '123'
      },
      mutations: {
        // 这里的state代表的是user里面的state
        updateToken(state) {
          state.token = '456789'
        }
      },
    },
    setting: {
      state: {
        name: '黑马'
      }
    }
  }
})

new Vue({
  // 5、挂载实例化
  store,
  render: h => h(App),
}).$mount('#app')